home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / GCLOOP.C < prev    next >
C/C++ Source or Header  |  1992-02-18  |  8KB  |  310 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /tmp/scm386/microcode/RCS/gcloop.c,v 9.39 1992/02/18 17:30:10 jinx Exp $
  4.  
  5. Copyright (c) 1987-1992 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* 
  36.  *
  37.  * This file contains the code for the most primitive part
  38.  * of garbage collection.
  39.  *
  40.  */
  41.  
  42. #include "scheme.h"
  43. #include "gccode.h"
  44.  
  45. /* Exports */
  46.  
  47. extern SCHEME_OBJECT * EXFUN (GCLoop, (SCHEME_OBJECT *, SCHEME_OBJECT **));
  48.  
  49. #define GC_Pointer(Code)                        \
  50. {                                    \
  51.   Old = OBJECT_ADDRESS (Temp);                        \
  52.   Code;                                    \
  53. }
  54.  
  55. #define Setup_Pointer_for_GC(Extra_Code)                \
  56. {                                    \
  57.   GC_Pointer(Setup_Pointer(true, Extra_Code));                \
  58. }
  59.  
  60. #ifdef ENABLE_GC_DEBUGGING_TOOLS
  61.  
  62. #ifndef GC_SCAN_HISTORY_SIZE
  63. #define GC_SCAN_HISTORY_SIZE 1024
  64. #endif
  65.  
  66. SCHEME_OBJECT
  67.   * gc_scan_trap = ((SCHEME_OBJECT *) 0),
  68.   * gc_free_trap = ((SCHEME_OBJECT *) 0),
  69.   gc_trap = (MAKE_OBJECT (TC_REFERENCE_TRAP, TRAP_MAX_IMMEDIATE)),
  70.   * (gc_scan_history [GC_SCAN_HISTORY_SIZE]),
  71.   * (gc_to_history [GC_SCAN_HISTORY_SIZE]);
  72.  
  73. static int gc_scan_history_index;
  74.  
  75. #define INITIALIZE_GC_HISTORY()                        \
  76. {                                    \
  77.   gc_scan_history_index = 0;                        \
  78.   {                                    \
  79.     SCHEME_OBJECT ** scan = gc_scan_history;                \
  80.     SCHEME_OBJECT ** end = (scan + GC_SCAN_HISTORY_SIZE);        \
  81.     while (scan < end)                            \
  82.       (*scan++) = ((SCHEME_OBJECT *) 0);                \
  83.   }                                    \
  84.   {                                    \
  85.     SCHEME_OBJECT ** scan = gc_to_history;                \
  86.     SCHEME_OBJECT ** end = (scan + GC_SCAN_HISTORY_SIZE);        \
  87.     while (scan < end)                            \
  88.       (*scan++) = ((SCHEME_OBJECT *) 0);                \
  89.   }                                    \
  90. }
  91.  
  92. #define HANDLE_GC_TRAP()                        \
  93. {                                    \
  94.   (gc_scan_history [gc_scan_history_index]) = Scan;            \
  95.   (gc_to_history [gc_scan_history_index]) = To;                \
  96.   if ((++gc_scan_history_index) == GC_SCAN_HISTORY_SIZE)        \
  97.     gc_scan_history_index = 0;                        \
  98.   if ((Temp == gc_trap)                            \
  99.       || ((gc_scan_trap != 0) && (Scan >= gc_scan_trap))        \
  100.       || ((gc_free_trap != 0) && (To >= gc_free_trap)))            \
  101.     {                                    \
  102.       fprintf(stderr, "\nGCLoop: trap.\n");                \
  103.       abort ();                                \
  104.     }                                    \
  105. }
  106.  
  107. #else
  108.  
  109. #define INITIALIZE_GC_HISTORY()
  110. #define HANDLE_GC_TRAP()
  111.  
  112. #endif
  113.  
  114. SCHEME_OBJECT *
  115. DEFUN (GCLoop,
  116.        (Scan, To_Pointer),
  117.        fast SCHEME_OBJECT * Scan
  118.        AND SCHEME_OBJECT ** To_Pointer)
  119. {
  120.   fast SCHEME_OBJECT *To, *Old, Temp, *Low_Constant, New_Address;
  121.  
  122.   INITIALIZE_GC_HISTORY ();
  123.   To = *To_Pointer;
  124.   Low_Constant = Constant_Space;
  125.   for ( ; Scan != To; Scan++)
  126.   {
  127.     Temp = *Scan;
  128.     HANDLE_GC_TRAP();
  129.  
  130.     Switch_by_GC_Type(Temp)
  131.     {
  132.       case TC_BROKEN_HEART:
  133.         if (Scan == (OBJECT_ADDRESS (Temp)))
  134.     {
  135.       *To_Pointer = To;
  136.       return (Scan);
  137.     }
  138.     sprintf(gc_death_message_buffer,
  139.         "gcloop: broken heart (0x%lx) in scan",
  140.         Temp);
  141.     gc_death(TERM_BROKEN_HEART, gc_death_message_buffer, Scan, To);
  142.     /*NOTREACHED*/
  143.  
  144.       case TC_MANIFEST_NM_VECTOR:
  145.       case TC_MANIFEST_SPECIAL_NM_VECTOR:
  146.     Scan += OBJECT_DATUM (Temp);
  147.     break;
  148.  
  149.       /* Compiled code relocation. */
  150.  
  151.       case TC_LINKAGE_SECTION:
  152.       {
  153.     switch (READ_LINKAGE_KIND (Temp))
  154.     {
  155.       case REFERENCE_LINKAGE_KIND:
  156.       case ASSIGNMENT_LINKAGE_KIND:
  157.       {
  158.         /* Assumes that all others are objects of type TC_QUAD without
  159.            their type codes.
  160.            */
  161.  
  162.         fast long count;
  163.  
  164.         Scan++;
  165.         for (count = READ_CACHE_LINKAGE_COUNT(Temp);
  166.          --count >= 0;
  167.          Scan += 1)
  168.         {
  169.           Temp = *Scan;
  170.           Setup_Pointer_for_GC(Transport_Quadruple());
  171.         }
  172.         Scan -= 1;
  173.         break;
  174.       }
  175.  
  176.       case OPERATOR_LINKAGE_KIND:
  177.       case GLOBAL_OPERATOR_LINKAGE_KIND:
  178.       {
  179.         fast long count;
  180.         fast char *word_ptr;
  181.         SCHEME_OBJECT *end_scan;
  182.  
  183.         START_OPERATOR_RELOCATION (Scan);
  184.         count = (READ_OPERATOR_LINKAGE_COUNT (Temp));
  185.         word_ptr = (FIRST_OPERATOR_LINKAGE_ENTRY (Scan));
  186.         end_scan = (END_OPERATOR_LINKAGE_AREA (Scan, count));
  187.  
  188.         while(--count >= 0)
  189.         {
  190.           Scan = ((SCHEME_OBJECT *) word_ptr);
  191.           word_ptr = (NEXT_LINKAGE_OPERATOR_ENTRY (word_ptr));
  192.           EXTRACT_OPERATOR_LINKAGE_ADDRESS (Temp, Scan);
  193.           GC_Pointer(Setup_Internal(true,
  194.                     Transport_Compiled(),
  195.                     Compiled_BH(true,
  196.                             goto next_operator)));
  197.           next_operator:
  198.           STORE_OPERATOR_LINKAGE_ADDRESS (Temp, Scan);
  199.         }
  200.         Scan = end_scan;
  201.         END_OPERATOR_RELOCATION (Scan);
  202.         break;
  203.       }
  204.  
  205.       default:
  206.       {
  207.         gc_death (TERM_EXIT,
  208.               "GC: Unknown compiler linkage kind.",
  209.               Scan, Free);
  210.         /*NOTREACHED*/
  211.       }
  212.     }
  213.     break;
  214.       }
  215.  
  216.       case TC_MANIFEST_CLOSURE:
  217.       {
  218.     fast long count;
  219.     fast char *word_ptr;
  220.     SCHEME_OBJECT *area_end;
  221.  
  222.     START_CLOSURE_RELOCATION (Scan);
  223.     Scan += 1;
  224.     count = (MANIFEST_CLOSURE_COUNT (Scan));
  225.     word_ptr = (FIRST_MANIFEST_CLOSURE_ENTRY (Scan));
  226.     area_end = (MANIFEST_CLOSURE_END (Scan, count));
  227.  
  228.     while ((--count) >= 0)
  229.     {
  230.       Scan = ((SCHEME_OBJECT *) (word_ptr));
  231.       word_ptr = (NEXT_MANIFEST_CLOSURE_ENTRY (word_ptr));
  232.       EXTRACT_CLOSURE_ENTRY_ADDRESS (Temp, Scan);
  233.       GC_Pointer(Setup_Internal(true,
  234.                     Transport_Compiled(),
  235.                     Compiled_BH(true, goto next_closure)));
  236.     next_closure:
  237.       STORE_CLOSURE_ENTRY_ADDRESS (Temp, Scan);
  238.     }
  239.  
  240.     Scan = area_end;
  241.     END_CLOSURE_RELOCATION (Scan);
  242.     break;
  243.       }
  244.  
  245.       case_compiled_entry_point:
  246.     GC_Pointer(Setup_Internal(true,
  247.                   Transport_Compiled(),
  248.                   Compiled_BH(true, goto after_entry)));
  249.       after_entry:
  250.     *Scan = Temp;
  251.     break;
  252.  
  253.       case_Cell:
  254.     Setup_Pointer_for_GC(Transport_Cell());
  255.     break;
  256.  
  257.       case TC_REFERENCE_TRAP:
  258.     if (OBJECT_DATUM (Temp) <= TRAP_MAX_IMMEDIATE)
  259.     {
  260.       /* It is a non pointer. */
  261.       break;
  262.     }
  263.     /* Fall Through. */
  264.  
  265.       case_Pair:
  266.     Setup_Pointer_for_GC(Transport_Pair());
  267.     break;
  268.  
  269.       case TC_VARIABLE:
  270.       case_Triple:
  271.     Setup_Pointer_for_GC(Transport_Triple());
  272.     break;
  273.  
  274.       case_Quadruple:
  275.     Setup_Pointer_for_GC(Transport_Quadruple());
  276.     break;
  277.  
  278.       case TC_BIG_FLONUM:
  279.     Setup_Pointer_for_GC({
  280.       Transport_Flonum();
  281.       break;
  282.     });
  283.  
  284.       case_Vector:
  285.     Setup_Pointer_for_GC(Transport_Vector());
  286.     break;
  287.  
  288.       case TC_FUTURE:
  289.     Setup_Pointer_for_GC(Transport_Future());
  290.     break;
  291.  
  292.       case TC_WEAK_CONS:
  293.     Setup_Pointer_for_GC(Transport_Weak_Cons());
  294.     break;
  295.  
  296.       default:
  297.     GC_BAD_TYPE("gcloop");
  298.     /* Fall Through */
  299.  
  300.       case_Non_Pointer:
  301.     break;
  302.  
  303.       }    /* Switch_by_GC_Type */
  304.   } /* For loop */
  305.  
  306.   *To_Pointer = To;
  307.   return (To);
  308.  
  309. } /* GCLoop */
  310.